/*   1:    */ package nl.siegmann.epublib.fileset;
/*   2:    */ 
/*   3:    */ import java.io.File;
/*   4:    */ import java.io.IOException;
/*   5:    */ import java.util.ArrayList;
/*   6:    */ import java.util.Arrays;
/*   7:    */ import java.util.Comparator;
/*   8:    */ import java.util.List;
/*   9:    */ import nl.siegmann.epublib.bookprocessor.DefaultBookProcessorPipeline;
/*  10:    */ import nl.siegmann.epublib.domain.Book;
/*  11:    */ import nl.siegmann.epublib.domain.Resource;
/*  12:    */ import nl.siegmann.epublib.domain.Resources;
/*  13:    */ import nl.siegmann.epublib.domain.Spine;
/*  14:    */ import nl.siegmann.epublib.domain.TOCReference;
/*  15:    */ import nl.siegmann.epublib.domain.TableOfContents;
/*  16:    */ import nl.siegmann.epublib.epub.BookProcessor;
/*  17:    */ import nl.siegmann.epublib.service.MediatypeService;
/*  18:    */ import nl.siegmann.epublib.util.ResourceUtil;
/*  19:    */ import nl.siegmann.epublib.util.VFSUtil;
/*  20:    */ import org.apache.commons.vfs.FileName;
/*  21:    */ import org.apache.commons.vfs.FileObject;
/*  22:    */ import org.apache.commons.vfs.FileSystemManager;
/*  23:    */ import org.apache.commons.vfs.FileType;
/*  24:    */ import org.apache.commons.vfs.VFS;
/*  25:    */ 
/*  26:    */ public class FilesetBookCreator
/*  27:    */ {
/*  28: 36 */   private static Comparator<FileObject> fileComparator = new Comparator()
/*  29:    */   {
/*  30:    */     public int compare(FileObject o1, FileObject o2)
/*  31:    */     {
/*  32: 39 */       return o1.getName().getBaseName().compareToIgnoreCase(o2.getName().getBaseName());
/*  33:    */     }
/*  34:    */   };
/*  35: 43 */   private static final BookProcessor bookProcessor = new DefaultBookProcessorPipeline();
/*  36:    */   
/*  37:    */   public static Book createBookFromDirectory(File rootDirectory)
/*  38:    */     throws IOException
/*  39:    */   {
/*  40: 46 */     return createBookFromDirectory(rootDirectory, "UTF-8");
/*  41:    */   }
/*  42:    */   
/*  43:    */   public static Book createBookFromDirectory(File rootDirectory, String encoding)
/*  44:    */     throws IOException
/*  45:    */   {
/*  46: 51 */     FileObject rootFileObject = VFS.getManager().resolveFile("file:" + rootDirectory.getCanonicalPath());
/*  47: 52 */     return createBookFromDirectory(rootFileObject, encoding);
/*  48:    */   }
/*  49:    */   
/*  50:    */   public static Book createBookFromDirectory(FileObject rootDirectory)
/*  51:    */     throws IOException
/*  52:    */   {
/*  53: 56 */     return createBookFromDirectory(rootDirectory, "UTF-8");
/*  54:    */   }
/*  55:    */   
/*  56:    */   public static Book createBookFromDirectory(FileObject rootDirectory, String encoding)
/*  57:    */     throws IOException
/*  58:    */   {
/*  59: 68 */     Book result = new Book();
/*  60: 69 */     List<TOCReference> sections = new ArrayList();
/*  61: 70 */     Resources resources = new Resources();
/*  62: 71 */     processDirectory(rootDirectory, rootDirectory, sections, resources, encoding);
/*  63: 72 */     result.setResources(resources);
/*  64: 73 */     TableOfContents tableOfContents = new TableOfContents(sections);
/*  65: 74 */     result.setTableOfContents(tableOfContents);
/*  66: 75 */     result.setSpine(new Spine(tableOfContents));
/*  67:    */     
/*  68: 77 */     result = bookProcessor.processBook(result);
/*  69:    */     
/*  70: 79 */     return result;
/*  71:    */   }
/*  72:    */   
/*  73:    */   private static void processDirectory(FileObject rootDir, FileObject directory, List<TOCReference> sections, Resources resources, String inputEncoding)
/*  74:    */     throws IOException
/*  75:    */   {
/*  76: 83 */     FileObject[] files = directory.getChildren();
/*  77: 84 */     Arrays.sort(files, fileComparator);
/*  78: 85 */     for (int i = 0; i < files.length; i++)
/*  79:    */     {
/*  80: 86 */       FileObject file = files[i];
/*  81: 87 */       if (file.getType() == FileType.FOLDER)
/*  82:    */       {
/*  83: 88 */         processSubdirectory(rootDir, file, sections, resources, inputEncoding);
/*  84:    */       }
/*  85: 89 */       else if (MediatypeService.determineMediaType(file.getName().getBaseName()) != null)
/*  86:    */       {
/*  87: 92 */         Resource resource = VFSUtil.createResource(rootDir, file, inputEncoding);
/*  88: 93 */         if (resource != null)
/*  89:    */         {
/*  90: 96 */           resources.add(resource);
/*  91: 97 */           if (MediatypeService.XHTML == resource.getMediaType())
/*  92:    */           {
/*  93: 98 */             TOCReference section = new TOCReference(file.getName().getBaseName(), resource);
/*  94: 99 */             sections.add(section);
/*  95:    */           }
/*  96:    */         }
/*  97:    */       }
/*  98:    */     }
/*  99:    */   }
/* 100:    */   
/* 101:    */   private static void processSubdirectory(FileObject rootDir, FileObject file, List<TOCReference> sections, Resources resources, String inputEncoding)
/* 102:    */     throws IOException
/* 103:    */   {
/* 104:108 */     List<TOCReference> childTOCReferences = new ArrayList();
/* 105:109 */     processDirectory(rootDir, file, childTOCReferences, resources, inputEncoding);
/* 106:110 */     if (!childTOCReferences.isEmpty())
/* 107:    */     {
/* 108:111 */       String sectionName = file.getName().getBaseName();
/* 109:112 */       Resource sectionResource = ResourceUtil.createResource(sectionName, VFSUtil.calculateHref(rootDir, file));
/* 110:113 */       resources.add(sectionResource);
/* 111:114 */       TOCReference section = new TOCReference(sectionName, sectionResource);
/* 112:115 */       section.setChildren(childTOCReferences);
/* 113:116 */       sections.add(section);
/* 114:    */     }
/* 115:    */   }
/* 116:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.fileset.FilesetBookCreator
 * JD-Core Version:    0.7.0.1
 */